package com.coollang.smartbra.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * @Auther: Timmy
 */

public class SportAnalyzeView extends View {
    private static final int[] colors = {0xFFFDEADB, 0xFFFCD4B4, 0xFFFABF8F, 0xFFF89545};
    private static final String[] texts = {"训练新手", "有些生疏", "定期训练", "训练达人"};

    /**
     * 进度条颜色
     */
    private int progressColor = 0xFFf66B12;

    /**
     * 绘制提示框的矩形
     */
    private RectF rectF = new RectF();

    /**
     * 画三角形的path
     */
    private Path path = new Path();

    /**
     * 评分等级
     */
    private int level = 0;

    private int mViewWith;
    private int mViewHeight;
    private int mLineHeight;
    private int mPadding;
    private int mIntervalHeight;
    private int mTipPaintWidth;
    private Paint mTipPaint;


    public SportAnalyzeView(Context context) {
        super(context);
    }

    public SportAnalyzeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        initPaint();
    }

    private void init() {
        mLineHeight = dp2px(50);
        mPadding = dp2px(20);
        mIntervalHeight = dp2px(35);
        mTipPaintWidth = dp2px(1);
    }

    private void initPaint() {
        mTipPaint = getPaint(mTipPaintWidth, progressColor, Paint.Style.FILL);
    }

    private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(style);
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // drawText(canvas);

        drawLine(canvas);
        drawNumber(canvas);
        drawTexts(canvas);
    }

    private void drawText(Canvas canvas) {
        Paint paint = new Paint();

        canvas.drawText("这里是画笔1", 0, 60, paint);
        paint.setTextSize(30);
        canvas.drawText("这里是画笔2", 0, 30, paint);
    }

    // 画直线
    private void drawLine(Canvas canvas) {
        Paint paint = new Paint();
        int length = colors.length;
        int shadePosition = (mViewWith - dp2px(40)) / length;
        paint.setStrokeWidth(dp2px(10));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        // paint.setStrokeCap(Paint.Cap.ROUND);

        for (int i = 0; i < length; i++) {
            // 直线的左边间距
            int padingLeft = 0;

            if (i == 0) {
                padingLeft = mPadding;
            }

            paint.setColor(colors[i]);
            int startX = shadePosition * i + mPadding;
            canvas.drawLine(startX, mLineHeight, startX + shadePosition + padingLeft, mLineHeight, paint);
        }
    }

    private void drawTexts(Canvas canvas) {
        Paint paint = new Paint();
        int length = texts.length;
        // 每一段的长度
        int shadePosition = (mViewWith - dp2px(40)) / length;
        paint.setAntiAlias(true);
        paint.setColor(0XFF666666);
        paint.setTextSize(sp2px(13));
        paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

        for (int i = 0; i < length; i++) {

            // 直线的左边间距
            int padingLeft = 0;
            int padingRight = 0;
            if (i == 0) {
                //padingLeft = mPadding / 2;
                padingLeft = mPadding;
            } else if (i == length - 1) {
                padingRight = mPadding / 2;
            }

            String text = texts[i];
            int textWith = getTextWidth(paint, text);
            int textHeight = getTextHeight(paint, text);

            int startX = shadePosition * i + mPadding;

            /**
             * 具体点的做法是求线段中点,然后减去一半的字体宽度
             */
            int x = ((startX + shadePosition + padingLeft) - startX) / 2 + startX - mPadding - padingRight / 2 - padingLeft / 2;
            int y = mPadding;

            if (i == level) {
                // 绘制圆角矩形
                drawRoundRect(canvas, x, y, textWith, textHeight);

                // 选中时候的字体颜色
                paint.setColor(0XDDFFFFFF);
            } else {
                // 未选中时候的字体颜色
                paint.setColor(0XFF666666);
            }

            canvas.drawText(text, x, y, paint);
        }
    }

    private void drawRoundRect(Canvas canvas, int x, int y, int textWith, int textHeight) {
        rectF.set(x - dp2px(6), y - textHeight - dp2px(1), x + textWith + dp2px(6), y + dp2px(5));

        int roundRectRadius = dp2px(2);
        canvas.drawRoundRect(rectF, roundRectRadius, roundRectRadius, mTipPaint);


        // 求椭圆中点的坐标
        int currentX = ((x + textWith + dp2px(6)) - (x - dp2px(6))) / 2 + (x - dp2px(6));

        drawTriangle(canvas, currentX, y);
    }

    /**
     * 绘制三角形
     */
    private void drawTriangle(Canvas canvas, int currentX, int y) {
        // 画三角形
        int triangleHeight = dp2px(10);

        path.moveTo(currentX - triangleHeight / 2, y + dp2px(5));
        path.lineTo(currentX, y + dp2px(5) + triangleHeight);
        path.lineTo(currentX + triangleHeight / 2, y + dp2px(5));
        canvas.drawPath(path, mTipPaint);
        path.reset();
    }

    private void drawNumber(Canvas canvas) {
        Paint paint = new Paint();
        int length = colors.length;

        if (length > 0) {
            int shadePosition = (mViewWith - dp2px(40)) / length;
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setColor(0XFF666666);
            paint.setTextSize(sp2px(20));

            for (int i = 0; i < length + 1; i++) {

                String str = i + 1 + "";
                canvas.drawText(str, 0 + mPadding - (getTextWidth(paint, str) / 2) + (shadePosition * i), mLineHeight + mIntervalHeight, paint);
            }
        }
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWith = w;
        mViewHeight = h;
    }

    //3. 精确计算文字的宽度：
    public static int getTextWidth(Paint mPaint, String str) {
        float iSum = 0;
        if (str != null && !str.equals("")) {
            int len = str.length();
            float widths[] = new float[len];
            mPaint.getTextWidths(str, widths);
            for (int i = 0; i < len; i++) {
                iSum += Math.ceil(widths[i]);
            }
        }
        return (int) iSum;
    }

    // 选中的行数
    public void setLevel(int level) {
        this.level = level;
        invalidate();
    }

    // 精确计算文字的高度
    public static int getTextHeight(Paint mPaint, String text) {
        Rect rect = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

    /**
     * dp 2 px
     */
    protected int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getResources().getDisplayMetrics());
    }

    /**
     * sp 2 px
     */
    protected int sp2px(int spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, getResources().getDisplayMetrics());
    }
}
